package com.qnbc.mystery.controller;

import com.alipay.api.internal.util.AlipaySignature;
import com.qnbc.mystery.config.AliPayConfig;
import com.qnbc.mystery.pojo.domain.FoodOrder;
import com.qnbc.mystery.pojo.dto.AlipayDto;
import io.swagger.annotations.Api;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.apache.catalina.manager.Constants.CHARSET;

/**
 * @author gzj
 * @description
 * @date 2024/1/14 11:40
 */

@RestController
@RequestMapping("/alipay")
@Slf4j
public class AlipayController {

    @Autowired
    private AliPayConfig aliPayConfig;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public void pay(AlipayDto dto, HttpServletResponse httpServletResponse) throws IOException {
        String pay = aliPayConfig.pay(dto.getId(), dto.getPrice(), dto.getTitle());
        httpServletResponse.setContentType("text/html;charset=" + CHARSET);
        httpServletResponse.getWriter().write(pay);// 直接将完整的表单html输出到页面
        httpServletResponse.getWriter().flush();
        httpServletResponse.getWriter().close();
    }



//    @PostMapping("/notify")  // 注意这里必须是POST接口
//    public String payNotify(HttpServletRequest request) throws Exception {
//        if ("TRADE_SUCCESS".equals(request.getParameter("trade_status"))) {
//            log.info("=========支付宝异步回调========");
//
//            Map<String, String> params = new HashMap<>();
//            Map<String, String[]> requestParams = request.getParameterMap();
//            for (String name : requestParams.keySet()) {
//                params.put(name, request.getParameter(name));
//                // System.out.println(name + " = " + request.getParameter(name));
//            }
//
//            String tradeNo = params.get("out_trade_no");
//            String gmtPayment = params.get("gmt_payment");
//            String alipayTradeNo = params.get("trade_no");
//
//            String sign = params.get("sign");
//            String content = AlipaySignature.getSignCheckContentV1(params);
//            // 支付宝验签
//            boolean checkSignature = AlipaySignature.rsa256CheckContent(content, sign, aliPayConfig.getAlipayPublicKey(), "UTF-8");
//            if (checkSignature) {
//                // 验签通过
//                System.out.println("交易名称: " + params.get("subject"));
//                System.out.println("交易状态: " + params.get("trade_status"));
//                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
//                System.out.println("商户订单号: " + params.get("out_trade_no"));
//                System.out.println("交易金额: " + params.get("total_amount"));
//                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
//                System.out.println("买家付款时间: " + params.get("gmt_payment"));
//                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
//
//                // 更新订单未已支付
//                FoodOrder FoodorderInfo = orderService.selectOrderById(Long.valueOf(tradeNo));
//                FoodorderInfo.setAlipayTraceNo(alipayTradeNo);
//                FoodorderInfo.setStatus(2L);
//                orderService.updateById(FoodorderInfo);
//            }
//        }
//        return "success";
//    }

}
